#!/bin/bash
# jdk 11 安装
init_jdk() {
  echo "安装jdk11"
  cd /app/pmapp/data/rongshu/jdk11/ || exit
  echo "jdk环境变量"
  new_content="export JAVA_HOME=/app/pmapp/data/rongshu/jdk11"
  new_content_1="export CLASSPATH=\$JAVA_HOME/lib:\$CLASSPATH"
  new_content_2="export PATH=\$JAVA_HOME/bin:\$PATH"
  echo "往配置文件最后一行添加配置"
  echo -e "$new_content\n$new_content_1\n$new_content_2" >>~/.bash_profile
  echo "加载配置"
  source /app/pmapp/.bash_profile
  echo "验证jdk安装版本"
  echo "$(java -version)"
}

# 启动 zk
init_zk() {
  zk_content="export ZK_HOME=/app/pmapp/data/rongshu/apache-zookeeper-3.8.4-bin/"
  zk_content_1="export PATH=\$PATH:\$JAVA_HOME/bin:\$ZK_HOME/bin"
  echo "往配置文件最后一行添加配置"
  echo -e "$zk_content\n$zk_content_1" >>~/.bash_profile
  echo "使配置文件生效"
  source /app/pmapp/.bash_profile
  # 后台启动zk
  cd /app/pmapp/data/rongshu/apache-zookeeper-3.8.4-bin/bin/ || exit
  zkServer.sh start
  echo "查看zk状态"
  echo "$(zkServer.sh status)"
}

# 初始化元数据-a集群
function


initialize_cluster_metadata_a() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar initialize-cluster-metadata \
    --cluster pulsar-service-1 \
    --zookeeper 10.91.6.47:2181,10.91.6.48:2181,10.91.6.49:2181 \
    --configuration-store 10.91.6.47:2181,10.91.6.48:2181,10.91.6.49:2181 \
    --web-service-url http://10.91.6.47:8080,10.91.6.48:8080,10.91.6.49:8080 \
    --web-service-url-tls https://10.91.6.47:8443,10.91.6.48:8443,10.91.6.49:8443 \
    --broker-service-url pulsar://10.91.6.47:6650,10.91.6.48:6650,10.91.6.49:6650 \
    --broker-service-url-tls pulsar+ssl://10.91.6.47:6651,10.91.6.48:6651,10.91.6.49:6651
  # 接着初始化bookkeeper集群：若出现提示输入Y/N：请输入Y
  echo "Y" | bin/bookkeeper shell metaformat
}

# 初始化元数据-b集群
function initialize_cluster_metadata_b() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar initialize-cluster-metadata \
    --cluster pulsar-service-1 \
    --zookeeper 10.102.3.153:2181,10.102.3.154:2181,10.102.3.155:2181 \
    --configuration-store 10.102.3.153:2181,10.102.3.154:2181,10.102.3.155:2181 \
    --web-service-url http://10.102.3.153:8080,10.102.3.154:8080,10.102.3.155:8080 \
    --web-service-url-tls https://10.102.3.153:8443,10.102.3.154:8443,10.102.3.155:8443 \
    --broker-service-url pulsar://10.102.3.153:6650,10.102.3.154:6650,10.102.3.155:6650 \
    --broker-service-url-tls pulsar+ssl://10.102.3.153:6651,10.102.3.154:6651,10.102.3.155:6651
  # 接着初始化bookkeeper集群：若出现提示输入Y/N：请输入Y
  echo "Y" | bin/bookkeeper shell metaformat
}

# start bookie
function start_bookie() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar-daemon start bookie
  echo "验证是否启动成功"
  echo "$(bin/bookkeeper shell bookiesanity)"
}

# start broker
function   start_broker() {
  echo "启动broker"
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar-daemon start broker
  echo "验证是否启动成功"
  echo "$(bin/pulsar-admin brokers list pulsar-service-1)"
}

# start proxy
function start_proxy() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar-daemon start proxy
}

# start pulsar manager
function start_pulsar_manager() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/pulsar-manager/pulsar-manager || exit
  nohup bin/pulsar-manager >pulsar-manager.log 2>&1 &
  sleep 10s
  echo "sh initAccount.sh"
  echo -e "pulsar_manager平台访问链接: \c"
  info "https://10.91.6.48:7750/ui/index.html"
}

deploy_jdk_zk() {
  init_jdk
  init_zk
}

deploy_initialize_cluster_metadata_a() {
  initialize_cluster_metadata_a
}

deploy_initialize_cluster_metadata_b() {
  initialize_cluster_metadata_b
}

deploy_bookie() {
  start_bookie
}

deploy_broker() {
  start_broker
}

deploy_proxy() {
  start_proxy
}

deploy_manage() {
  start_pulsar_manager
}

# 如何使用
case $1 in
1) deploy_jdk_zk ;;
2) deploy_initialize_cluster_metadata_a ;;
3) deploy_bookie ;;
4) deploy_broker ;;
5) deploy_proxy ;;
6) deploy_manage ;;
7) deploy_initialize_cluster_metadata_7 ;;
*)
  echo "pulsar_a集群节点配置文件修改 $0 1(jdk-zk安装-通用)/2(初始化元数据-集群A)/3(启动bookie节点-通用)/4(启动broker节点-通用)/5(启动proxy节点-通用)/6(启动manage节点-通用)/6(初始化元数据-集群B)"
  ;;
esac
